/**
 * 
 */
package cx3k.GUI;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;

/**
 * @author Chris
 *
 */
public class ProfilePane extends JPanel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField jtfName;
	private JComboBox jcbMajor, jcbPlaceholder, jcbEmpty;
	private CompletedClasses FinishedClasses;
	private JLabel jlCCValue, jlSCValue;
	private Scheduler scheduler;
	private String[] majors, minors;
	private JRadioButton jrbNone, jrbMajor, jrbMinor;
	private ButtonGroup jrbGroup;
	private Box addBox;
	private RadioListener RL;
	private ProfileListener PL;
	
	public ProfilePane(Scheduler s) {
		GridBagLayout gbl = new GridBagLayout();
		this.setLayout(gbl);
		GridBagConstraints gbc = new GridBagConstraints();
		this.setBackground(MainWindow.MAINCOLOR);
		this.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
		this.scheduler = s;
		majors = s.getMajors();
		minors = s.getMinors();
		this.PL = new ProfileListener();
		
		JLabel jlName = new JLabel("Name:");
		gbc.gridx = 0;
		gbc.gridy = 0; 
		gbc.gridwidth = 1; 
		gbc.gridheight = 1; 
		gbc.weightx = 30; 
		gbc.weighty = 10; 
		gbc.fill = GridBagConstraints.NONE; 
		gbc.anchor = GridBagConstraints.LINE_START; 
		gbl.setConstraints(jlName, gbc);
		this.add(jlName); 
		
		this.jtfName = new JTextField();
		this.jtfName.addActionListener(PL);
		gbc.gridx = 1;
		gbc.gridy = 0; 
		gbc.gridwidth = 1; 
		gbc.gridheight = 1; 
		gbc.weightx = 70; 
		gbc.weighty = 10; 
		gbc.fill = GridBagConstraints.HORIZONTAL; 
		gbc.anchor = GridBagConstraints.LINE_START; 
		gbl.setConstraints(jtfName, gbc);
		this.add(jtfName); 
		
		JLabel jlMajor = new JLabel("Primary Major:");
		gbc.gridx = 0;
		gbc.gridy = 1; 
		gbc.gridwidth = 1; 
		gbc.gridheight = 1; 
		gbc.weightx = 30; 
		gbc.weighty = 10; 
		gbc.fill = GridBagConstraints.NONE; 
		gbc.anchor = GridBagConstraints.LINE_START; 
		gbl.setConstraints(jlMajor, gbc);
		this.add(jlMajor); 
	
		this.jcbMajor = new JComboBox();
		jcbMajor.addActionListener(PL);
		addToComboBox(majors, jcbMajor);
		jcbMajor.setCursor(new Cursor(Cursor.HAND_CURSOR));
		gbc.gridx = 1;
		gbc.gridy = 1; 
		gbc.gridwidth = 1; 
		gbc.gridheight = 1; 
		gbc.weightx = 70; 
		gbc.weighty = 10; 
		gbc.fill = GridBagConstraints.HORIZONTAL; 
		gbc.anchor = GridBagConstraints.LINE_START; 
		gbl.setConstraints(jcbMajor, gbc);
		this.add(jcbMajor);

		JLabel jlMinors = new JLabel("Additional Major or Minor:");
		gbc.gridx = 0;
		gbc.gridy = 2; 
		gbc.gridwidth = 1; 
		gbc.gridheight = 1; 
		gbc.weightx = 70; 
		gbc.weighty = 10; 
		gbc.fill = GridBagConstraints.NONE; 
		gbc.anchor = GridBagConstraints.LINE_START; 
		gbl.setConstraints(jlMinors, gbc);
		this.add(jlMinors);
		
		RL = new RadioListener();
		jrbGroup = new ButtonGroup();
		addBox = Box.createHorizontalBox();
		
		jrbNone = new JRadioButton("None", true);
		initRB(jrbNone);
		jrbMajor = new JRadioButton("Major", false);
		initRB(jrbMajor);
		jrbMinor = new JRadioButton("Minor", false);
		initRB(jrbMinor);
		
		jcbEmpty = new JComboBox();
		jcbEmpty.setEnabled(false);
		
		
		jcbPlaceholder = jcbEmpty;
		jcbPlaceholder.setCursor(new Cursor(Cursor.HAND_CURSOR));
		jcbPlaceholder.addActionListener(PL);
		addBox.add(jcbPlaceholder);
		
		gbc.gridx = 1;
		gbc.gridy = 2; 
		gbc.gridwidth = 1; 
		gbc.gridheight = 1; 
		gbc.weightx = 70; 
		gbc.weighty = 10; 
		gbc.fill = GridBagConstraints.HORIZONTAL; 
		gbc.anchor = GridBagConstraints.LINE_START; 
		gbl.setConstraints(addBox, gbc);
		this.add(addBox); 
		
		
		JLabel c = new JLabel("Completed Classes:");
		gbc.gridx = 0;
		gbc.gridy = 3; 
		gbc.gridwidth = 1; 
		gbc.gridheight = 1; 
		gbc.weightx = 10; 
		gbc.weighty = 5; 
		gbc.fill = GridBagConstraints.NONE; 
		gbc.anchor = GridBagConstraints.LINE_START; 
		gbl.setConstraints(c, gbc);
		this.add(c);
		
		this.FinishedClasses = new CompletedClasses();
		JScrollPane scrollClasses = new JScrollPane(FinishedClasses);
		gbc.gridx = 0;
		gbc.gridy = 4; 
		gbc.gridwidth = 2; 
		gbc.gridheight = 2; 
		gbc.weightx = 100; 
		gbc.weighty = 50; 
		gbc.fill = GridBagConstraints.BOTH; 
		gbc.anchor = GridBagConstraints.LINE_START; 
		gbl.setConstraints(scrollClasses, gbc);
		this.add(scrollClasses);
		
		JLabel jlCCredits = new JLabel("Completed Credits:  ");
		this.jlCCValue = new JLabel("0");
		JLabel jlSCredits = new JLabel("Scheduled Credits:  ");
		this.jlSCValue = new JLabel("0");
		
		Box hBox = Box.createHorizontalBox();
		hBox.add(jlCCredits);
		hBox.add(jlCCValue);
		hBox.add(Box.createHorizontalStrut(200));
		hBox.add(jlSCredits);
		hBox.add(jlSCValue);
		
		gbc.gridx = 0;
		gbc.gridy = 6; 
		gbc.gridwidth = 2; 
		gbc.gridheight = 1; 
		gbc.weightx = 10; 
		gbc.weighty = 10; 
		gbc.fill = GridBagConstraints.NONE; 
		gbc.anchor = GridBagConstraints.LINE_START; 
		gbl.setConstraints(hBox, gbc);
		this.add(hBox);
		
		
	}
	
	private void addToComboBox(String s[], JComboBox jcb) {
		jcb.addItem(" ");
		for (int i = 0; i < s.length; i++) {
			jcb.addItem(s[i]);
		}
	}
	
	private void initRB(JRadioButton jrb) {
		jrb.setBackground(MainWindow.MAINCOLOR);
		jrb.addActionListener(RL);
		jrb.setCursor(new Cursor(Cursor.HAND_CURSOR));
		jrbGroup.add(jrb);
		addBox.add(jrb);
	}
	
	private class RadioListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent ae) {
			// TODO Auto-generated method stub
			
			if (ae.getSource() == jrbNone) {
				jcbPlaceholder.removeAllItems();
				jcbPlaceholder.setEnabled(false);
				scheduler.updateAddType(1);
			}
			else if (ae.getSource() == jrbMajor) {
				jcbPlaceholder.setEnabled(true);
				jcbPlaceholder.removeAllItems();
				addToComboBox(majors, jcbPlaceholder);
				scheduler.updateAddType(2);
			}
			else if (ae.getSource() == jrbMinor) {
				jcbPlaceholder.setEnabled(true);
				jcbPlaceholder.removeAllItems();
				addToComboBox(minors, jcbPlaceholder);
				scheduler.updateAddType(3);
			}
		}
		
	}

	private class ProfileListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent ae) {
			// TODO Auto-generated method stub
			if (ae.getSource() == jtfName) {
				scheduler.updateName(jtfName.getText());
			}
			else if (ae.getSource() == jcbMajor) {
					scheduler.updateMajor(jcbMajor.getSelectedIndex()-1);
			}
			else if (ae.getSource() == jcbPlaceholder) {
					scheduler.updateMajor(jcbPlaceholder.getSelectedIndex()-1);
			}
		}
		
	}
	
	public String getName() { return jtfName.getText(); }
	public void setName(String name) { jtfName.setText(name); }
	
	public String getMajor() { return jcbMajor.getSelectedItem().toString(); }
	
	public int getCCValue() { return Integer.parseInt(jlCCValue.getText()); }
	public void setCCValue(int credits) { jlCCValue.setText(new Integer(credits).toString()); }
	
	public int getSCValue() { return Integer.parseInt(jlSCValue.getText()); }
	public void setSCValue(int credits) { jlSCValue.setText(new Integer(credits).toString()); }
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}